<?php namespace Phphub\Vote;

use App\Models\Reply;
use App\Models\Topic;
use App\Models\Vote;
use App\Models\User;
use App\Models\TranslateRecord;
use App\Models\Notification;
use Carbon\Carbon;
use Auth;
use DB;
use App\Activities\UserUpvotedTopic;
use App\Activities\UserUpvotedReply;
use App\Activities\UserUpvotedTransRecord;
use League\Flysystem\Exception;

class Voter
{
    public $notifiedUsers = [];

    public function topicUpVote(Topic $topic)
    {

        if ($topic->votes()->ByWhom(Auth::id())->WithType('upvote')->count())
        {
            // click twice for remove upvote
            $vote = $topic->votes()->ByWhom(Auth::id())->WithType('upvote');
            $flag = 1;

        } elseif ($topic->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $vote = $topic->votes()->ByWhom(Auth::id())->WithType('downvote');
            $flag = 2;
        } else {
            // first time click

            $flag = 3;
        }
        $user = Auth::user();
        try
        {
            DB::transaction(function () use(&$vote,&$topic, &$user, $flag) {
                if($flag == 1)
                {
                    $vote->delete();
                    $topic->decrement('vote_count', 1);
                    $topic->user->decrement('vote_count', 1);
                    app(UserUpvotedTopic::class)->remove($user, $topic);
                }
                elseif($flag == 2)
                {
                    $vote->delete();
                    $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);

                    $topic->increment('vote_count', 2);
                    $topic->user->increment('vote_count', 2);
                    app(UserUpvotedTopic::class)->generate($user, $topic);
                }
                elseif($flag == 3)
                {
                    $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
                    $topic->increment('vote_count', 1);
                    $topic->user->increment('vote_count', 1);
                    app(UserUpvotedTopic::class)->generate($user, $topic);
                    Notification::notify('topic_upvote', $user, $topic->user, $topic);
                }

                if ($topic->isShareLink()) {
                    $topic->updated_at = Carbon::now()->toDateTimeString();
                    $topic->save();
                }
            });
        }
        catch (Exception $e)
        {

        }

        return $topic;
    }

    public function topicDownVote(Topic $topic)
    {

        if ($topic->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // click second time for remove downvote
            $vote = $topic->votes()->ByWhom(Auth::id())->WithType('downvote');
            $flag = 1;

        } elseif ($topic->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // user already clicked upvote once
            $vote = $topic->votes()->ByWhom(Auth::id())->WithType('upvote');
            $flag = 2;

        } else {
            // click first time
            $flag = 3;
        }

        try
        {
            DB::transaction(function () use(&$vote, &$topic, $flag) {
                if($flag == 1)
                {
                    $vote->delete();
                    $topic->increment('vote_count', 1);
                    $topic->user->decrement('vote_count', 1);
                }
                elseif($flag == 2)
                {
                    $vote->delete();
                    $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'downvote']);
                    $topic->decrement('vote_count', 2);
                    $topic->user->increment('vote_count', 2);
                }
                elseif($flag == 3)
                {
                    $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'downvote']);
                    $topic->decrement('vote_count', 1);
                    $topic->user->increment('vote_count', 1);
                }
            });
        }
        catch (Exception $e)
        {

        }
    }

    public function replyUpVote(Reply $reply)
    {
        if (Auth::id() == $reply->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }

        $return = [];
        if ($reply->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $vote = $reply->votes()->ByWhom(Auth::id())->WithType('upvote');
            $flag = 1;

        } elseif ($reply->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $vote = $reply->votes()->ByWhom(Auth::id())->WithType('downvote');
            $flag = 2;

        } else {
            // first time click
            $flag = 3;
        }

        try
        {
            DB::transaction(function () use($flag, &$vote, &$reply,&$return) {
                if($flag == 1)
                {
                    $vote->delete();
                    $reply->decrement('vote_count', 1);
                    $return['action_type'] = 'sub';
                    app(UserUpvotedReply::class)->remove(Auth::user(), $reply);
                }
                elseif($flag == 2)
                {
                    $vote->delete();
                    $reply->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
                    $reply->increment('vote_count', 2);
                    $return['action_type'] = 'add';
                    app(UserUpvotedReply::class)->generate(Auth::user(), $reply);
                }
                elseif($flag == 3)
                {
                    $reply->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
                    $reply->increment('vote_count', 1);
                    $return['action_type'] = 'add';

                    Notification::notify('reply_upvote', Auth::user(), $reply->user, $reply->topic, $reply);
                    app(UserUpvotedReply::class)->generate(Auth::user(), $reply);
                }
            });
        }
        catch (Exception $e)
        {

        }
        return $return;
    }

    public function transRecordUpVote(TranslateRecord $record)
    {
//        if (Auth::id() == $record->translator_id) {
//            return \Flash::warning(lang('Can not vote your feedback'));
//        }

        $return = [];

        if ($record->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $vote = $record->votes()->ByWhom(Auth::id())->WithType('upvote');
            $flag = 1;

        } elseif ($record->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $vote = $record->votes()->ByWhom(Auth::id())->WithType('downvote');
            $flag = 2;

        } else {
            // first time click
            $flag = 3;
        }

        try
        {

            DB::transaction(function () use($flag, &$vote, &$record,&$return) {
                if($flag == 1)
                {
                    $vote->delete();
                    $record->decrement('vote_count', 1);
                    $return['action_type'] = 'sub';
                    app(UserUpvotedTransRecord::class)->remove(Auth::user(), $record);
                }
                elseif($flag == 2)
                {
                    $vote->delete();
                    $record->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
                    $record->increment('vote_count', 2);
                    $return['action_type'] = 'add';
                    app(UserUpvotedTransRecord::class)->generate(Auth::user(), $record);
                }
                elseif($flag == 3)
                {
                    $record->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
                    $record->increment('vote_count', 1);
                    $return['action_type'] = 'add';

                    Notification::notify('translate_record_upvote', Auth::user(), $record->user, $record->translateSection->topic);
                    app(UserUpvotedTransRecord::class)->generate(Auth::user(), $record);
                }
            });
        }
        catch (Exception $e)
        {
        }
        return $return;
    }
}
